﻿using BCCommon.Enums;
using BCDto.Common.UploadFileRecord;
using BCDto.UploadHandleComponent;
using BCService.Common.UploadFileRecord;
using BCService.WeChatComponent.WeChatKeyAccountService;
using BCService.WeChatComponent.WeChatMaterialService.WeChatMaterialService;
using BCService.WeChatComponent.WeChatMaterialService.WeChatNewsMaterialService;
using System;
using System.DrawingCore;

namespace BCService.UploadHandleComponent.ImageHandle
{
    /// <summary>
    /// 图片处理
    /// </summary>
    public class ImageHandle : IUploadHandle
    {
        private readonly IUploadFileRecordService uploadFileRecordService;
        private readonly IWeChatNewsMaterialService weChatNewsMaterialService;
        private readonly IWeChatKeyAccountService weChatKeyAccountService;
        private readonly IWeChatMaterialService weChatMaterialService;
        private UploadFileDto uploadFileDto;

        public ImageHandle(IUploadFileRecordService uploadFileRecordService,
            IWeChatNewsMaterialService weChatNewsMaterialService,
            IWeChatKeyAccountService weChatKeyAccountService,
            IWeChatMaterialService weChatMaterialService)
        {
            this.uploadFileRecordService = uploadFileRecordService;
            this.weChatNewsMaterialService = weChatNewsMaterialService;
            this.weChatKeyAccountService = weChatKeyAccountService;
            this.weChatMaterialService = weChatMaterialService;
        }


        #region 处理文件
        /// <summary>
        /// 处理文件
        /// </summary>
        /// <param name="uploadFileDto">上传文件模型</param>
        /// <returns></returns>
        public UploadResponseDto Process(UploadFileDto uploadFileDto)
        {
            this.uploadFileDto = uploadFileDto;
            SystemTask();
            string medialId = ExtensionTask();
            return new UploadResponseDto()
            {
                Message = medialId
            };
        }
        #endregion

        #region 检查数据
        /// <summary>
        /// 检查数据
        /// </summary>
        /// <param name="uploadFileDto"></param>
        public void CheckData(UploadFileDto uploadFileDto)
        {
            this.uploadFileDto = uploadFileDto;
            if (uploadFileDto.Usage == UploadUsageType.WeChat)
            {
                switch (uploadFileDto.RemarkType)
                {
                    case UploadFileRemarkType.WeChatNewsMaterial:
                        this.weChatNewsMaterialService.WeChatNewsMaterialCheckData(this.uploadFileDto);
                        break;
                    case UploadFileRemarkType.WeChatKeyAccountAvatar:
                        if (uploadFileDto.FileSuffix != ".jpg")
                        {
                            throw new ArgumentException("微信客服头像的图片格式必须是JPG格式。");
                        }
                        break;
                    case UploadFileRemarkType.WeChatPermanentMaterial:
                    case UploadFileRemarkType.WeChatTemporaryMaterial:
                    case UploadFileRemarkType.WeChatPermanentThumbnailMaterial:
                    case UploadFileRemarkType.WeChatTemporaryThumbnailMaterial:
                        this.weChatMaterialService.WeChatMaterialCheckData(this.uploadFileDto);
                        break;
                }
            }
        }


        #endregion

        #region 作业任务
        #region 系统作业
        /// <summary>
        /// 系统作业
        /// </summary>
        private void SystemTask()
        {
            if (uploadFileDto.RemarkType != UploadFileRemarkType.WeChatNewsMaterial)
            {
                ImageProcess imageProcess = new ImageProcess();
                using (Image image = Image.FromFile(this.uploadFileDto.FilePath))
                {
                    imageProcess.CreateImageThumbnail(this.uploadFileDto.FileName, this.uploadFileDto.FileSuffix, 50, 50, image, this.uploadFileDto.DirectoryPath, out string thumbnailPath);
                }
            }
        }
        #endregion

        #region 扩展作业
        /// <summary>
        /// 扩展作业
        /// </summary>
        private string ExtensionTask()
        {
            string medialId = string.Empty;
            if (uploadFileDto.Usage == UploadUsageType.WeChat)
            {
                switch (uploadFileDto.RemarkType)
                {
                    case UploadFileRemarkType.WeChatNewsMaterial:
                        string response = weChatNewsMaterialService.UploadWeChatNewsMaterial(this.uploadFileDto);
                        AddUploadRecord();
                        return response;
                    case UploadFileRemarkType.WeChatKeyAccountAvatar:
                        weChatKeyAccountService.UploadAvatar(this.uploadFileDto.Remarks["kf_account"].ToString(), this.uploadFileDto.FilePath);
                        break;
                    case UploadFileRemarkType.WeChatPermanentMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, true);
                        break;
                    case UploadFileRemarkType.WeChatTemporaryMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, false);
                        break;
                    case UploadFileRemarkType.WeChatPermanentThumbnailMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, true);
                        break;
                    case UploadFileRemarkType.WeChatTemporaryThumbnailMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, false);
                        break;
                }
            }
            AddUploadRecord();
            return medialId;
        }
        #endregion


        #endregion

        #region 上传图片记录
        /// <summary>
        /// 添加上传记录
        /// </summary>
        private void AddUploadRecord()
        {
            UploadFileRecordDto uploadFileRecordDto = new UploadFileRecordDto();
            uploadFileRecordDto.UploadFileRecordNo = this.uploadFileDto.FileName;
            uploadFileRecordDto.FileSize = this.uploadFileDto.ContentLength;
            uploadFileRecordDto.FilePath = this.uploadFileDto.DirectoryPath;
            uploadFileRecordDto.FileUrl = this.uploadFileDto.Src;
            uploadFileRecordDto.FileSuffix = this.uploadFileDto.FileSuffix;
            uploadFileRecordDto.FileUsage = this.uploadFileDto.Usage.ToString();
            uploadFileRecordDto.RemarkType = this.uploadFileDto.Usage.Equals("Self") ? "" : this.uploadFileDto.RemarkType.ToString();
            uploadFileRecordDto.WebsiteName = this.uploadFileDto.WebsiteName;
            uploadFileRecordDto.WebsiteFileDirectory = this.uploadFileDto.WebsiteFileDirectory;
            uploadFileRecordDto.WebsiteFullName = this.uploadFileDto.WebsiteFullName;
            this.uploadFileRecordService.Add(uploadFileRecordDto);
        }
        #endregion
    }
}
